package com.cedarhd.brzx.domain.service;

import com.cedarhd.brzx.common.enums.MemberMethodEnum;
import com.cedarhd.brzx.dto.request.*;
import org.hibernate.annotations.common.util.StringHelper;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author Evan
 * @date 2018/4/26
 */
@Service
public class SoaMemberService extends SoaBaseService{
    private static Logger log = LoggerFactory.getLogger(SoaMemberService.class);

    private static String soaName = "MemberService";

    /**
     * 开通会员
     * @param dto
     * @return
     */
    public JSONObject createMember(CreateMemberDTO dto){
        if(dto == null){
            return null;
        }

        try {
            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("memberType", dto.getMemberType().getValue());
            param.put("source", dto.getSource().getValue());
            param.put("extendParam", new JSONObject());

            JSONObject response = client.request(soaName, MemberMethodEnum.CREATE_MEMBER.getValue(), param);
            log.info("createMember response = {}",response);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("createMember error,",e);
        }
        return null;
    }

    /**
     * 发送短信验证码
     * @param dto
     * @return
     */
    public JSONObject sendVerificationCode(SendVerificationCodeDTO dto){
        if(dto == null){
            return null;
        }

        try {
            JSONObject extendParams = new JSONObject();
            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("phone", dto.getPhone());
            param.put("verificationCodeType", dto.getVerificationCodeType().getValue());
            param.put("extendParams", extendParams);

            JSONObject response = client.request(soaName, MemberMethodEnum.SEND_VERIFICATION_CODE.getValue(), param);
            log.info("sendVerificationCode response = {}",response);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("sendVerificationCode error,",e);
        }
        return null;
    }

    /**
     * 绑定手机号
     * @param dto
     * @return
     */
    public JSONObject bindPhone(BindPhoneDTO dto){
        if(dto == null){
            return null;
        }

        try{
            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("phone", dto.getPhone());
            param.put("verificationCode", dto.getVerificationCode());

            JSONObject response = client.request(soaName, MemberMethodEnum.BIND_PHONE.getValue(),param);
            log.info("bindPhone response = {}",response);
            return response;
        }catch(Exception e){
            e.printStackTrace();
            log.error("bindPhone error, e={}",e);
        }
        return null;
    }

    /**
     * 修改绑定手机号
     * @param dto
     * @return
     */
    public JSONObject changeBindPhone(ChangeBindPhoneDTO dto){
        if(dto == null){
            return null;
        }

        try{
            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("oldPhone", dto.getOldPhone());
            param.put("newPhone",dto.getNewPhone());
            param.put("newVerificationCode", dto.getNewVerificationCode());

            JSONObject response = client.request(soaName, MemberMethodEnum.CHANGE_BIND_PHONE.getValue(),param);
            log.info("changeBindPhone response = {}",response);
            return response;
        }catch(Exception e){
            e.printStackTrace();
            log.error("changeBindPhone error, e={}",e);
        }
        return null;

    }

    /**
     * 个人实名认证
     *
     * @param realAuthDTO
     * @return
     */
    public JSONObject realNameAuthentication(RealAuthDTO realAuthDTO) {
        if (realAuthDTO == null) {
            return null;
        }
        try {
            JSONObject param = new JSONObject();
            param.put("bizUserId", realAuthDTO.getBizUserId());
            param.put("isAuth",true);
            param.put("name", realAuthDTO.getName());
            //param.put("identityType", realAuthDTO.getIdentityType().getValue());
            param.put("identityType", 1L);//目前只支持身份证类型
            param.put("identityNo", rsaEncrypt(realAuthDTO.getIdentityNo()));
            log.info("setRealName request = {}",param);
            JSONObject response = client.request(soaName, MemberMethodEnum.SET_REALNAME.getValue(), param);
            log.info("setRealName response = {}",response);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("setRealName error,",e);
        }
        return null;
    }

    /**
     * 设置企业信息
     * @param dto
     * @return
     */
    public JSONObject enterpriseInformation(EnterpriseInformationDTO dto) {
        if (dto == null) {
            return null;
        }
        try {
            log.info("enterpriseInformation start");
            JSONObject companyBasicInfo = new JSONObject();
            CompanyBasicInfoDTO companyBasicInfoDTO = dto.getCompanyBasicInfo();
            companyBasicInfo.put("companyName",companyBasicInfoDTO.getCompanyName());
            companyBasicInfo.put("companyAddress", companyBasicInfoDTO.getCompanyAddress());
            //companyBasicInfo.put("authType", companyBasicInfoDTO.getAuthType().getValue());
            companyBasicInfo.put("authType", 1L);
            companyBasicInfo.put("uniCredit", companyBasicInfoDTO.getUniCredit());
            companyBasicInfo.put("businessLicense", companyBasicInfoDTO.getBusinessLicense());
            companyBasicInfo.put("organizationCode", companyBasicInfoDTO.getOrganizationCode());
            companyBasicInfo.put("taxRegister", companyBasicInfoDTO.getTaxRegister());
            companyBasicInfo.put("expLicense",companyBasicInfoDTO.getExpLicens());
            companyBasicInfo.put("telephone", companyBasicInfoDTO.getTelephone());
            companyBasicInfo.put("legalName", companyBasicInfoDTO.getLegalName());
            //companyBasicInfo.put("identityType",companyBasicInfoDTO.getIdentityType().getValue());
            companyBasicInfo.put("identityType",1L);
            companyBasicInfo.put("legalIds", rsaEncrypt(companyBasicInfoDTO.getLegalIds()));
            companyBasicInfo.put("legalPhone", companyBasicInfoDTO.getLegalPhone());
            companyBasicInfo.put("accountNo", rsaEncrypt(companyBasicInfoDTO.getAccountNo()));
            companyBasicInfo.put("parentBankName", companyBasicInfoDTO.getParentBankName());
            companyBasicInfo.put("bankCityNo", companyBasicInfoDTO.getBankCityNo());
            companyBasicInfo.put("bankName", companyBasicInfoDTO.getBankName());
            companyBasicInfo.put("unionBank", companyBasicInfoDTO.getUnionBank());

            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("backUrl",dto.getBackUrl());
            param.put("companyBasicInfo", companyBasicInfo);
            param.put("isAuth",dto.getIsAuth());

            log.info("request:{}" , param);
            JSONObject response = client.request(soaName, "setCompanyInfo", param);
            log.info("response:{}" , response);
            log.info("enterpriseInformation end");
            return response;
        } catch (Exception e) {
            log.info("enterpriseInformation error!", e);
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 企业信息审核结果通知
     * @param verifyResultDTO
     * @return
     */
    public JSONObject verifyResult(VerifyResultDTO verifyResultDTO){
        String  rps = verifyResultDTO.getRps();
        return new JSONObject(rps).getJSONObject("returnValue");
    }


    /**
     * 查询会员信息
     * @param bizUserId
     * @return
     */
    public JSONObject getMemberInfo(String bizUserId){
        try{
            JSONObject param = new JSONObject();
            param.put("bizUserId", bizUserId);

            JSONObject response = client.request(soaName, MemberMethodEnum.GET_MEMBER_INFO.getValue(),param);
            log.info("getMemberInfo response = {}",response);
            return response;
        }catch(Exception e){
            e.printStackTrace();
            log.error("getMemberInfo error, e={}",e);
        }
        return null;
    }

    /**
     * 查询卡bin
     * @param dto
     * @return
     */
    public JSONObject getBindBankBin(GetBankCardBinRequestDTO dto){

        JSONObject response = null;
        try{
            log.info("testGetBankCardBin start");
            JSONObject param = new JSONObject();
            param.put("cardNo", rsaEncrypt(dto.getCardNo()));
            log.info("request:" + param);
            response = client.request(soaName, "getBankCardBin", param);
            log.info("response:" + response);
            log.info("testGetBankCardBin end");
        }catch(Exception e){
            log.error("testGetBankCardBin error, e={}", e);
            e.printStackTrace();
        }

        return  response;
    }

    /**
     * 查询卡bin
     * @param dto
     * @return
     */
    public JSONObject applyBindBankCard(ApplyBindBankCardRequestDto dto){

        JSONObject response = null;

        try{
            //借记卡
            JSONObject param = new JSONObject();

            param.put("bizUserId", dto.getBizUserId());
            param.put("cardNo", rsaEncrypt(dto.getCardNo()));
            param.put("phone",  dto.getPhone());
            param.put("name", dto.getName());
            param.put("cardCheck", 2L);
            param.put("identityType", 1L);
            param.put("identityNo", rsaEncrypt(dto.getIdentityNo()));
            param.put("validate", "");
            param.put("cvv2", "");
            param.put("isSafeCard", false);
            param.put("unionBank", "");




            log.info("applyBindBankCard request start ************");
            log.info("param ={}", param.toString());
            response =  client.request(soaName, "applyBindBankCard", param);
            log.info("response:" + response);

            log.info("testApplyBindBankCard end**********");
        }catch(Exception e){
            log.error("testApplyBindBankCard error, e={}", e);
            e.printStackTrace();
        }

        return  response;
    }


    /**
     * 绑定银行卡
     * @param dto
     * @return
     */
    public JSONObject bindBankCard(BindBankCardRequestDto dto){

        JSONObject response = null;
        try{
            System.out.println("bindBankCard start");

            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("tranceNum", dto.getTranceNum());
            param.put("transDate", dto.getTranceDate());
            param.put("phone", dto.getPhone());
            param.put("verificationCode", dto.getVerificationCode());

            log.info("bindBankCard request:" + param);
            response = client.request(soaName, "bindBankCard", param);
            log.info("bindBankCard response:" + response);

            log.info("bindBankCard  end");
        }catch(Exception e){
            log.error("bindBankCard  error, e={}", e);
            e.printStackTrace();
        }

        return  response;
    }



    /**
     * 设置安全卡
     * @param dto
     * @return
     */
    public JSONObject setSafeCard(SetSafeCardRequestDto dto){

        JSONObject response = null;

        try{
            log.info("setSafeCard start");
            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("cardNo",rsaEncrypt(dto.getCardNo()));

            log.info("request:" + param);
            response = client.request(soaName, "setSafeCard", param);
            log.info("response:" + response);

            log.info("setSafeCard end");
        }catch(Exception e){
            log.error("setSafeCard error, e={}", e);
            e.printStackTrace();
        }

        return  response;
    }


    /**
     * 查询银行卡
     * @param dto
     * @return
     */
    public JSONObject queryBankCard(QueryBankCardRequestDto dto){

        JSONObject response = null;

        try{
            System.out.println("queryBankCard start");

            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            if(StringHelper.isNotEmpty(dto.getCardNo())){
                param.put("cardNo",rsaEncrypt(dto.getCardNo()));
            }

            log.info("request:" + param);
            response = client.request(soaName, "queryBankCard", param);
            log.info("response:" + response);

            log.info("queryBankCard end");
        }catch(Exception e){
            log.error("queryBankCard error, e={}", e);
            e.printStackTrace();
        }

        return  response;
    }

    /**
     * 解绑银行卡
     * @param dto
     * @return
     */
    public JSONObject unbindBankCard(UnbindBankCardRequestDto dto){

        JSONObject response = null;
        try{
            System.out.println("unbindBankCard start");

            JSONObject param = new JSONObject();
            param.put("bizUserId", dto.getBizUserId());
            param.put("cardNo", rsaEncrypt(dto.getCardNo()));
            log.info("request:" + param);
            response = client.request(soaName, "unbindBankCard", param);
            log.info("response:" + response);
            log.info("unbindBankCard end");
        }catch(Exception e){
            log.error("unbindBankCard error, e={}", e);
            e.printStackTrace();
        }
        return  response;
    }

}
